 /*
 *  *Target：不断检测M0板的环境信息，并将数据写入sendEnv结构体中
 *  *Author：许文强
 *  *Date：2017.7.18
 *    
 */
#include "project.h"



void serial_init(int fd)
{
	struct termios options;
	tcgetattr(fd, &options);
	options.c_cflag |= ( CLOCAL | CREAD );
	options.c_cflag &= ~CSIZE;
	options.c_cflag &= ~CRTSCTS;
	options.c_cflag |= CS8;
	options.c_cflag &= ~CSTOPB;
	options.c_iflag |= IGNPAR;
	options.c_iflag &= ~(ICRNL | IXON);
	options.c_oflag = 0;
	options.c_lflag = 0;
	cfsetispeed(&options, B115200);
	cfsetospeed(&options, B115200);
	tcsetattr(fd,TCSANOW,&options);
}

void *pthread_data_collection()
{
	
		
	int ret = 0;
	int dev_uart_fd;
	if((dev_uart_fd = open("/dev/ttyUSB0", O_RDWR)) < 0)
	{
		perror("open zigbee error");
		exit(1);
	}


	serial_init(dev_uart_fd);    //节点的初始化
	int i = 0;
	while(1)
	{

	
		pthread_mutex_lock(&mutex);
		printf("%d-----read_lock-------\n",++i);
		if(ret = read(dev_uart_fd, &myEnv, sizeof(myEnv)) < 0)    //将节点中的数据读入到结构体中
		{
			perror("pthread_data_collection:fail to read");
			exit(1);

		}
		printf("%d\n",ret);
		printf("data_collection success!!!\n");
		printf("collection:%uF,%uF,%u%%,%u%%,%um/(s*s),%um/(s*s),%um/(s*s),%uLUX,%umV,%umV\n", myEnv.temp[0], myEnv.temp[1], myEnv.hum[0], myEnv.hum[1], myEnv.x, myEnv.y, myEnv.z, myEnv.ill, myEnv.bet, myEnv.adc);
		pthread_cond_broadcast(&notempty);
		pthread_mutex_unlock(&mutex);
		usleep(2000);
	}


}
